/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerCadastraEntrada;
import java.util.Date;
import model.Cliente;
import controller.ControllerCadastraCliente;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import model.ComandaCabecalho;
import model.Evento;

/**
 *
 * @author Dick developer
 */
public class JRecepcao extends javax.swing.JFrame {

    private ControllerCadastraEntrada cce;
    private ControllerCadastraCliente controllercadastracliente;
    private JPrincipal jp;
    private String cpf;
    private Cliente cliente;
    //private Calendar datanasc;
    private Date date;
    private int tamanho;
    private int cont = 1;
    private boolean alterar;
    private int id_cliente;
    private ComandaCabecalho comanda;
    
    
    
    
    
    
    public JRecepcao(ControllerCadastraEntrada cce,JPrincipal jp) {
        initComponents();
        this.jp = jp;
        this.cce = cce;
        controllercadastracliente = new ControllerCadastraCliente(jp);
        cad_cli.setVisible(false);
        id_cliente = 0;
        
        
        DefaultComboBoxModel model = (DefaultComboBoxModel)cb_unidade.getModel();
        for(Evento e: this.cce.listaComEventos()){        
            model.addElement(e);
        }  
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        a = new javax.swing.JLabel();
        ta_nome = new javax.swing.JFormattedTextField();
        d = new javax.swing.JLabel();
        c = new javax.swing.JLabel();
        b = new javax.swing.JLabel();
        documento = new javax.swing.JFormattedTextField();
        ta_sexo = new javax.swing.JFormattedTextField();
        ta_fone = new javax.swing.JFormattedTextField();
        ta_nasc = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        bt_zerar = new javax.swing.JButton();
        cad_cli = new javax.swing.JButton();
        cb_unidade = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImages(null);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        jLabel1.setText("Cadastro de Cartão");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 60, -1, -1));

        jLabel2.setText("Evento:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 110, -1, -1));

        jLabel3.setText("Documento:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 150, -1, -1));

        jFormattedTextField2.setDocument(new util.NumberField(13));
        jFormattedTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextField2ActionPerformed(evt);
            }
        });
        getContentPane().add(jFormattedTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 110, 110, -1));

        a.setText("Nome:");
        getContentPane().add(a, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 190, -1, -1));

        ta_nome.setEnabled(false);
        getContentPane().add(ta_nome, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 190, 110, -1));

        d.setText("Sexo:");
        getContentPane().add(d, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 310, -1, -1));

        c.setText("Data de Nasciemento:");
        getContentPane().add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 270, -1, -1));

        b.setText("Telefone:");
        getContentPane().add(b, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 230, -1, -1));

        try {
            documento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        documento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                documentoActionPerformed(evt);
            }
        });
        documento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                documentoKeyReleased(evt);
            }
        });
        getContentPane().add(documento, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 150, 110, -1));

        ta_sexo.setEnabled(false);
        ta_sexo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ta_sexoActionPerformed(evt);
            }
        });
        getContentPane().add(ta_sexo, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 310, 110, -1));

        ta_fone.setEnabled(false);
        ta_fone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ta_foneActionPerformed(evt);
            }
        });
        getContentPane().add(ta_fone, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 230, 110, -1));

        ta_nasc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
        ta_nasc.setEnabled(false);
        ta_nasc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ta_nascActionPerformed(evt);
            }
        });
        getContentPane().add(ta_nasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 270, 110, -1));

        jLabel9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        jLabel9.setText("Sistema de recepção");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 30, -1, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fechar.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton1MouseExited(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 340, 60, 60));

        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton2MouseExited(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 340, 60, 60));

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cartao.png"))); // NOI18N
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 190, 230, 150));

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cpf.jpg"))); // NOI18N
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 150, 40, 30));

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cod_barras.png"))); // NOI18N
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 110, 40, 30));

        bt_zerar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/xpequeno.png"))); // NOI18N
        bt_zerar.setBorderPainted(false);
        bt_zerar.setContentAreaFilled(false);
        bt_zerar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bt_zerarMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                bt_zerarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                bt_zerarMouseExited(evt);
            }
        });
        bt_zerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_zerarActionPerformed(evt);
            }
        });
        getContentPane().add(bt_zerar, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 150, 30, 30));

        cad_cli.setForeground(new java.awt.Color(255, 255, 255));
        cad_cli.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cadasstro_cliente_cartao.png"))); // NOI18N
        cad_cli.setBorder(null);
        cad_cli.setBorderPainted(false);
        cad_cli.setContentAreaFilled(false);
        cad_cli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cad_cliMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cad_cliMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                cad_cliMouseExited(evt);
            }
        });
        cad_cli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cad_cliActionPerformed(evt);
            }
        });
        getContentPane().add(cad_cli, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 210, 60, 60));

        cb_unidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_unidadeActionPerformed(evt);
            }
        });
        getContentPane().add(cb_unidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 110, 160, -1));

        jLabel4.setText("Código barras do cartão:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 110, -1, -1));

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fundotelasinvertido.png"))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 700, 450));

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        jMenu1.setBackground(new java.awt.Color(255, 255, 255));
        jMenu1.setText("Ajuda");
        jMenuBar1.add(jMenu1);

        jMenu2.setBackground(new java.awt.Color(255, 255, 255));
        jMenu2.setText("Regras da casa");
        jMenuBar1.add(jMenu2);

        jMenu3.setBackground(new java.awt.Color(255, 255, 255));
        jMenu3.setText("Sair");

        jMenuItem1.setText("Sair");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jFormattedTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField2ActionPerformed

    private void documentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_documentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_documentoActionPerformed

    private void ta_sexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ta_sexoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ta_sexoActionPerformed

    private void ta_foneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ta_foneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ta_foneActionPerformed

    private void ta_nascActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ta_nascActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ta_nascActionPerformed

    private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseEntered

        jButton1.setSize(70,70);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseEntered

    private void jButton1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseExited

        jButton1.setSize(60,60);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseExited

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        jp.restaurar(jp);
        this.hide();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseEntered

        jButton2.setSize(70,70);

    }//GEN-LAST:event_jButton2MouseEntered

    private void jButton2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseExited

        jButton2.setSize(60,60);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseExited

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        boolean grava = true;
        
        if(jFormattedTextField2.getText().length() < 4 && jFormattedTextField2.getText().length() == 0){
            
            JOptionPane.showMessageDialog(null, "Código de barras invalido, favor verificar");
            grava = false;
        }
        
        
        
        if(grava == true){
        comanda = new ComandaCabecalho();
        comanda.setCliente(cliente);
        comanda.setCodigoBarras(jFormattedTextField2.getText());
        comanda.setEvento((Evento) cb_unidade.getSelectedItem());
        
        if(cce.cadastra_comanda(comanda)){
            JOptionPane.showMessageDialog(null, "Nova comanda cadastrada com sucesso. ");
            this.zera_tela();
        
        }else{
            
            JOptionPane.showMessageDialog(null, "Erro ao cadastrar. Tente novamente. ");
            
        }
        }
        
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
       jp.restaurar(jp);
       this.hide();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void documentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_documentoKeyReleased

        //JOptionPane.showMessageDialog(null, tamanho);
        
         if(documento.getText().length() == 4){
            cont =1;
        }
        
        while(cont ==1){
            tamanho = documento.getText().length();
            tamanho = (tamanho-11);
            cont = 2;
        }
        tamanho = (tamanho +1);
        
        if(tamanho >= 14){

            cpf = documento.getText();
            
            if(controllercadastracliente.verifica_se_usuario_tem_cadastro(cpf)){

                String data;
                
                cliente = new Cliente();
                cliente = controllercadastracliente.retorna_dados_do_cliente(cpf);
                id_cliente = cliente.getId();
                ta_nome.setText(cliente.getNome());
                ta_fone.setText(cliente.getTelefone());
                date = new Date();
                date = cliente.getNascimento();
                data = date.getDay()+"/"+date.getMonth()+"/"+date.getYear();  
                ta_nasc.setText(data);
                ta_sexo.setText(cliente.getSexo());
                
                JOptionPane.showMessageDialog(null, "Este usuario já esta cadastrado no sistema");
                bt_zerar.setVisible(true);
                voltatela();
                
            }else{
                
                limpatela();
                
                //JOptionPane.showMessageDialog(null, "Este usuario não tem cadastro ativo no sistema");
                
            }
        
        }
        
        // TODO add your handling code here:
    }//GEN-LAST:event_documentoKeyReleased

    private void bt_zerarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_zerarMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_zerarMouseClicked

    private void bt_zerarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_zerarMouseEntered
        bt_zerar.setSize(40,40);
    }//GEN-LAST:event_bt_zerarMouseEntered

    private void bt_zerarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bt_zerarMouseExited
        bt_zerar.setSize(30,30);
    }//GEN-LAST:event_bt_zerarMouseExited

    private void bt_zerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_zerarActionPerformed
        zera_tela();
    }//GEN-LAST:event_bt_zerarActionPerformed

    private void cad_cliMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cad_cliMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_cad_cliMouseClicked

    private void cad_cliMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cad_cliMouseEntered
        cad_cli.setSize(70,70);
    }//GEN-LAST:event_cad_cliMouseEntered

    private void cad_cliMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cad_cliMouseExited
        cad_cli.setSize(60,60);
    }//GEN-LAST:event_cad_cliMouseExited

    private void cad_cliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cad_cliActionPerformed
        // TODO add your handling code here
        
        ControllerCadastraCliente ccc = new ControllerCadastraCliente(this);
        ccc.iniciaApp_caixa();
        voltatela();
        zera_tela();
        
    }//GEN-LAST:event_cad_cliActionPerformed

    private void cb_unidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_unidadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_unidadeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel a;
    private javax.swing.JLabel b;
    private javax.swing.JButton bt_zerar;
    private javax.swing.JLabel c;
    private javax.swing.JButton cad_cli;
    private javax.swing.JComboBox cb_unidade;
    private javax.swing.JLabel d;
    private javax.swing.JFormattedTextField documento;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JFormattedTextField ta_fone;
    private javax.swing.JFormattedTextField ta_nasc;
    private javax.swing.JFormattedTextField ta_nome;
    private javax.swing.JFormattedTextField ta_sexo;
    // End of variables declaration//GEN-END:variables

public void exibe(){
    setVisible(true);
       
}

    private void limpatela() {
        
        a.setVisible(false);
        b.setVisible(false);
        c.setVisible(false);
        d.setVisible(false);
        ta_fone.setVisible(false);
        ta_nasc.setVisible(false);
        ta_nome.setVisible(false);
        ta_sexo.setVisible(false);
        jButton2.setVisible(false);
        cad_cli.setVisible(true);
        
        
    }

    private void voltatela() {
        
        a.setVisible(true);
        b.setVisible(true);
        c.setVisible(true);
        d.setVisible(true);
        ta_fone.setVisible(true);
        ta_nasc.setVisible(true);
        ta_nome.setVisible(true);
        ta_sexo.setVisible(true);
        jButton2.setVisible(true);
        cad_cli.setVisible(false);
        
        
    }
    
    
    private void zera_tela() {
        
        jFormattedTextField2.setText("");
        documento.setText("");
        ta_nome.setText("");
        ta_fone.setText("");
        ta_nasc.setText("");
        ta_sexo.setText("");
        cont =1;
        bt_zerar.setVisible(false);
        jButton2.setVisible(true);
        
    }

    public void restaurar(JRecepcao jr) {
        //this.jp = jf;
        setEnabled(true);

    }
    
    
}
